
#ifndef __lcd_show_character_c__
#define __lcd_show_character_c__ 1

#include "lcd19264.h"
#include "GT23L16U2W.h"

extern uint8_t font_dots_32[32];

// 纵排
void lcd_show_ucode_1516_v(uint8_t chx, uint8_t chy, uint8_t data[])
{
	int cs = chx / 4 + 1;
	int tx = chy * 2;
	int ty = (chx % 4) * 16;
	for (int i = 0; i < 32; i++)
	{
		lcd_show_uint8_on_cs(cs, tx + (i + 1) % 2, ty + i / 2, data[i]);
	}
}
// 横排
void lcd_show_ucode_1516_h(uint8_t chx, uint8_t chy, uint8_t datas[])
{
	int cs = chx / 4 + 1;
	int tx = chy * 2;
	int ty = (chx % 4) * 16;
	
	lcd_cs(cs);
	
	// 写两页
	for (int p = 0; p < 2; p++)
	{
		lcd_set_x(tx + p); // 页地址
		lcd_set_y(ty); // 列地址
		
		for (int y = 0; y < 16; y++) {
			uint8_t ydots = 0x00; // 要显示的8位点
			uint8_t data_idx = y / 8 + p * 16; // 从哪几个8位点计算来
			
			for (int b = 0; b < 8; b++)
			{
				//
				ydots >>= 1;
				if (datas[data_idx] & 0b10000000)
					ydots |= 0b10000000;
				//
				datas[data_idx] = datas[data_idx] << 1;
				data_idx += 2;
			}
			lcd_write_data(ydots);
		}
	}
}

void lcd_show_ucode_1516_at(uint8_t chx, uint8_t chy, uint16_t c, uint8_t font_dots_32[])
{
	uint32_t addr = font_addr_for_ucode_1516(c);
	font_read_dots_by_addr(addr, font_dots_32, 32);
	lcd_show_ucode_1516_h(chx, chy, font_dots_32);
}

// 横排
void lcd_show_ascii_78_h(uint8_t chx, uint8_t chy, uint8_t datas[])
{
	int cs = chx / 8 + 1;
	int tx = chy * 1;
	int ty = (chx % 8) * 8;
	
	lcd_cs(cs);
	lcd_set_x(tx); // 页地址
	lcd_set_y(ty); // 列地址
	
	for (int y = 0; y < 8; y++) {
		uint8_t ydots = 0x00; // 要显示的8位点
		uint8_t data_idx = 0; // 从哪几个8位点计算来
		
		for (int b = 0; b < 8; b++)
		{
			//
			ydots >>= 1;
			if (datas[data_idx] & 0b10000000)
				ydots |= 0b10000000;
			//
			datas[data_idx] = datas[data_idx] << 1;
			data_idx += 1;
		}
		lcd_write_data(ydots);
	}
}

void lcd_show_ascii_78_at(uint8_t chx, uint8_t chy, uint8_t c, uint8_t font_dots_8[])
{
	uint32_t addr = font_addr_for_ascii_78(c);
	font_read_dots_by_addr(addr, font_dots_8, 8);
	lcd_show_ascii_78_h(chx, chy, font_dots_8);
}
// 横排
void lcd_show_ascii_57_h(uint8_t chx, uint8_t chy, uint8_t datas[])
{
	int tx = chy * 1;
	int ty = chx * 6;
	
	for (int y = 0; y < 5; y++) {
		uint8_t ydots = 0x00; // 要显示的8位点
		uint8_t data_idx = 0; // 从哪几个8位点计算来
		
		for (int b = 0; b < 8; b++)
		{
			//
			ydots >>= 1;
			if (datas[data_idx] & 0b10000000)
				ydots |= 0b10000000;
			//
			datas[data_idx] = datas[data_idx] << 1;
			data_idx += 1;
		}
		lcd_show_uint8_at(tx, ty + y, ydots);
	}
}

void lcd_show_ascii_57_at(uint8_t chx, uint8_t chy, uint8_t c, uint8_t font_dots_8[])
{
	uint32_t addr = font_addr_for_ascii_57(c);
	font_read_dots_by_addr(addr, font_dots_8, 8);
	lcd_show_ascii_57_h(chx, chy, font_dots_8);
}

void lcd_show_ascii_57_at_pos(uint8_t chpos, uint8_t c, uint8_t font_dots_8[])
{
	uint32_t addr = font_addr_for_ascii_57(c);
	font_read_dots_by_addr(addr, font_dots_8, 8);
	lcd_show_ascii_57_h(chpos % 32, chpos / 32, font_dots_8);
}

#endif // __lcd_show_character_c__
